<?php
    error_reporting(E_ALL & ~E_DEPRECATED); 
    require_once("condb.php");     
    require_once("functions/functions_global.php");     
    require_once('libs/nusoap.php');
    
    $server = new soap_server();
    
    $ns = "urn:WappushServices";
    $server->configureWSDL("WappushServices", $ns);
    $server->wsdl->schemaTargetNamespace = $ns;    
    
    $server->wsdl->addComplexType(
        'response'
        , 'complexType'
        , 'struct'
        , 'all'
        , ''
        , array(
            'status'=>array(
                'name'=>'status'
                , 'type'=>'xsd:int'
            ),
            'msg'=>array(
                'name'=>'msg'
                , 'type'=>'xsd:string'
            )
        )
    );           
    
    $server->register(
        "call"
        , array("content"=>"xsd:string", "sender"=>"xsd:string", "serviceCode"=>"xsd:string", "transactionId"=>"xsd:string")
        , array("return"=> "tns:response")
//        , array("return"=> "xsd:string")
        , $ns
        , $ns . "#call"
        , "rpc"            
        , "encoded"
        , "Examination Results"  
    ); 
    
    function call($content, $sender, $serviceCode, $transactionId)
    {       
        $ip = getRealIpAddr();
        $IpPublic = IpPublic();
        $response = array();
        if(in_array($ip, $IpPublic)){
            
            $data = array();
            $data["content"] = preg_replace("/(\s+)/si", " ", $content);        
            $data["sender"] = $sender;
            $data["serviceCode"] = $serviceCode;
            $data["transactionId"] = $transactionId;    
            
            /* Kiểm tra transaction */
            $checkTransaction = CheckTransaction($transactionId);
            
            if($checkTransaction==0){
                $cp = false;
                
                if(preg_match("/^TAI/si", $data["content"])){                
                    
                    /*
                    * Beme
                    */
                    $cp = true;
                    require_once("functions/functions_content.php");                
                    
                }        
                
                if($cp == true){
                    $response = processing($data);    
                } else {
                    $response["status"] = 6;            
                    $response["msg"] = "Invalid Request";    
                }
                
                
            } else {
                $response["status"] = 4;            
                $response["msg"] = "Invalid Request";    
            }        
        } else {
            $response["status"] = 5;            
            $response["msg"] = "Invalid Request";                
        }       
            
        return $response;
    }            
    
    $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : ""; 
    $server->service($HTTP_RAW_POST_DATA);
    exit();
?>
